﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Data;
using OpenSIS.OpenSISWCFServiceReference;
using OSISToolTip;

namespace OpenSIS.School
{
    public partial class Period : System.Web.UI.Page
    {
        protected string sCulture;
        protected void Page_Load(object sender, EventArgs e)
        {
            //txtStartTime.Attributes.Add("onblur", "validateDatePicker(this)");
            //txtEndTime.Attributes.Add("onblur", "validateDatePicker(this)");
            TooltipLang TTL = new TooltipLang();
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                TTL.tooltip();
                ibtnAdd.Attributes.Add("onmouseover", "toolTip('" + TTL.Add + "')");
                ibtnAdd.Attributes.Add("onmouseout", "toolTip()");
                imgBtnEdit.Attributes.Add("onmouseover", "toolTip('" + TTL.Edit + "')");
                imgBtnEdit.Attributes.Add("onmouseout", "toolTip()");
                imgBtnDelete.Attributes.Add("onmouseover", "toolTip('" + TTL.Delete + "')");
                imgBtnDelete.Attributes.Add("onmouseout", "toolTip()");
            }
            //imgBtnEdit.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");

            //PeriodBL SPBL = new PeriodBL();
            //SchoolBL schoolAdd = new SchoolBL();

            if (!Page.IsPostBack)
            {
                //if (Session["SchoolID"] != null)
                //{
                //    ddlSchoolName.Items.FindByValue(Session["SchoolID"].ToString()).Selected = true;
                //}
             
                PopulateSchoolPeriodGridview();

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
              int length = 0;
              string UsedAtt = "";
              if (txtLength.Text != "")
                {
                    length = Convert.ToInt32(txtLength.Text);
                }

              if (ChkUsedAttendance.Checked)
                {
                   UsedAtt = "Y";
                }
                else
                {
                   UsedAtt = "N";
                }

            OpenSISWCFServicePeriodClient SPClient = new OpenSISWCFServicePeriodClient();

            try
            {
                SPClient.AddSchoolPeriod(Convert.ToInt32(Session["SchoolID"].ToString()), txtTitle.Text, txtShortName.Text, length, txtBlock.Text, UsedAtt,txtStartTime.Value,txtEndTime.Value);
            }
            catch
            {
            }
            finally
            {
                
            }
            SPClient.Close();
            PopulateSchoolPeriodGridview();
            txtStartTime.Value = "12:00 pm";
            txtEndTime.Value = "12:00 pm";

            //Function Func = new Function();
            //PeriodBL SPBL = new PeriodBL();
            //try
            //{


            //    ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
            //    SPBL.SchId = Convert.ToInt32(ddlSchoolName.SelectedItem.Value);
            //    SPBL.ShortName = txtShortName.Text.ToString();
            //    SPBL.Title = txtTitle.Text.ToString();

            //    SPBL.StartTime = txtStartTime.Value.ToString();
            //    SPBL.EndTime = txtEndTime.Value.ToString();
            //    if (txtLength.Text != "")
            //    {
            //        SPBL.Length = Convert.ToInt32(txtLength.Text);
            //    }
            //    SPBL.Block = txtBlock.Text.ToString();
            //    if (ChkUsedAttendance.Checked)
            //    {
            //        SPBL.UseAtted = "Y";
            //    }
            //    else
            //    {
            //        SPBL.UseAtted = "N";
            //    }
            //    if (Convert.ToInt32(ddlSchoolName.SelectedItem.Value) != 0 && txtShortName.Text.ToString() != "" && txtTitle.Text.ToString() != "" && txtLength.Text != "")
            //    {
            //        SPBL.AddSchoolPeriod();
            //    }
            //    Session["datatable"] = null;
            //    Func.ResetFormControlValues(this.Page);
           
            //}
            //catch
            //{

            //}
            //finally
            //{
            //    Func = null;
            //    SPBL = null;
            //}

        }

        protected void PopulateSchoolPeriodGridview()
        {

            DataTable dt = new DataTable();
            OpenSISWCFServicePeriodClient SPClient = new OpenSISWCFServicePeriodClient();

            dt = SPClient.PopulateSchoolPeriod(Convert.ToInt32(Session["SchoolID"]),"","","",0,"","");


          //  dt = SPClient.PopulateSchoolPeriod();

            if (dt.Rows.Count > 0)
            {
                rlperiod.DataSource = dt;
                rlperiod.DataBind();

                string s = System.Web.HttpContext.Current.Session["MLCulture"].ToString();

                switch (s)
                {
                    case "en-US":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Periods were found";
                        break;
                    case "es-MX":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Períodos se encontraron";
                        break;
                    case "zh-CN":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "期 是  创立";
                        break;
                    case "ja-JP":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "期間  なった  設立する";
                        break;
                    case "de-DE":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Zeitraum Gefunden";
                        break;
                    case "ko-KR":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "기간이 발견됐다";
                        break;
                    case "fr-FR":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Les périodes ont été trouvés";
                        break;
                    case "it-IT":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "I periodi sono stati trovati";
                        break;
                    case "ru-RU":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Периоды нашли";

                        break;
                    case "pt-BR":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "Períodos foram encontrados";
                        break;
                    case "ar-SA":
                        lblFoundCount.Text = rlperiod.Items.Count.ToString() + " " + "تم العثور على فترات";
                        break;
                }
            }
            else
            {
                rlperiod.DataBind();

                string s = System.Web.HttpContext.Current.Session["MLCulture"].ToString();

                switch (s)
                {
                    case "en-US":
                        lblFoundCount.Text = "No Periods were found";
                        break;
                    case "es-MX":
                        lblFoundCount.Text = "No Períodos se encontraron";
                        break;
                    case "zh-CN":
                        lblFoundCount.Text = "否 期 是  创立";
                        break;
                    case "ja-JP":
                        lblFoundCount.Text = "いいえ 期間  なった  設立する";
                        break;
                    case "de-DE":
                        lblFoundCount.Text = "Nein Zeitraum Gefunden";
                        break;
                    case "ko-KR":
                        lblFoundCount.Text = "아니오 기간이 발견됐다";
                        break;
                    case "fr-FR":
                        lblFoundCount.Text = "Non Les périodes ont été trouvés";
                        break;
                    case "it-IT":
                        lblFoundCount.Text = "No I periodi sono stati trovati";
                        break;
                    case "ru-RU":
                        lblFoundCount.Text = "Нет Периоды нашли";

                        break;
                    case "pt-BR":
                        lblFoundCount.Text = "Não Períodos foram encontrados";
                        break;
                    case "ar-SA":
                        lblFoundCount.Text = "تم العثور على فترات لا";
                        break;
                }



            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    PeriodBL SPBL = new PeriodBL();
            //    SPBL.Title = txtSTitle.Text.ToString().Trim();
            //    SPBL.ShortName = txtSShortName.Text.ToString().Trim();
            //    SPBL.ShortOrder = txtSShortOrder.Text.ToString().Trim();
            //    if (txtSLength.Text != "")
            //    {
            //        SPBL.Length = Convert.ToInt32(txtSLength.Text);
            //    }
            //    SPBL.Block = txtSBlock.Text.ToString().Trim();
            //    if (ChkUsedForAttendance.Checked)
            //    {
            //        SPBL.UseAtted = "Y";
            //    }
            //    else
            //    {
            //        SPBL.UseAtted = "N";
            //    }
            //    DataTable dt = new DataTable();

            //    ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
            //    SPBL.SchId = Convert.ToInt32(ddlSchoolName.SelectedItem.Value);

            //    dt = SPBL.PopulateSchoolPeriod();
            //    if (dt.Rows.Count > 0)
            //    {
            //        rlperiod.DataSource = dt;
            //        rlperiod.DataBind();
            //    }
            //    else
            //    {
            //        dt.Rows.Add(dt.NewRow());

            //        rlperiod.DataSource = dt;
            //        rlperiod.DataBind();
            //        int columnCount = rlperiod.Rows[0].Cells.Count;
            //        rlperiod.Rows[0].Cells.Clear();

            //        rlperiod.Rows[0].Cells.Add(new TableCell());
            //        rlperiod.Rows[0].Cells[0].ColumnSpan = columnCount;

            //        rlperiod.Rows[0].Cells[0].Text = "No data found";
            //        rlperiod.Rows[0].Cells[0].BackColor = System.Drawing.Color.WhiteSmoke;
            //    }
            //}
            //catch
            //{

            //}
        }

        protected void imgBtnEdit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                foreach (ReorderListItem RI in rlperiod.Items)
                {
                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        if (chk.Checked)
                        {
                            HiddenField hdnPeriodId = (HiddenField)RI.FindControl("hdnPeriodId");
                            HiddenField hdnUFA = (HiddenField)RI.FindControl("hdnUFA");
                            Label lblTitle = (Label)RI.FindControl("lblTitle");

                            Session["PeriodId"] = hdnPeriodId.Value.ToString();
                            txtTitleEdit.Text = lblTitle.Text.ToString();
                            Label lblShortName = (Label)RI.FindControl("lblShortName");
                            Label lblStartTime = (Label)RI.FindControl("lblStartTime");
                            Label lblEndTime = (Label)RI.FindControl("lblEndTime");
                            Label lblLength = (Label)RI.FindControl("lblLength");
                            Label lblBlock = (Label)RI.FindControl("lblBlock");
                            txtShortNameEdit.Text = lblShortName.Text;

                            txtStartTimeEdit.Value = lblStartTime.Text;
                            txtEndTimeEdit.Value = lblEndTime.Text;
                            txtLengthEdit.Text = lblLength.Text;
                            txtBlockEdit.Text = lblBlock.Text;
                            txtEndTimeEdit.Value = lblEndTime.Text;
                            if (hdnUFA.Value.ToString().Trim() == "Y")
                            {
                                ChkUsedAttenEdit.Checked = true;
                            }
                            else
                            {
                                ChkUsedAttenEdit.Checked = false;
                            }
                        }
                    }
                }

                if (rlperiod.Items.Count == 1)
                {
                    if (lblFoundCount.Text == "No Periods were found" || lblFoundCount.Text == "No Períodos se encontraron" || lblFoundCount.Text == "否 期 是  创立" || lblFoundCount.Text == "いいえ 期間  なった  設立する" || lblFoundCount.Text == "Nein Zeitraum Gefunden" || lblFoundCount.Text == "아니오 기간이 발견됐다" || lblFoundCount.Text == "Non Les périodes ont été trouvés" || lblFoundCount.Text == "No I periodi sono stati trovati" || lblFoundCount.Text == "Нет Периоды нашли" || lblFoundCount.Text == "Não Períodos foram encontrados" || lblFoundCount.Text == "تم العثور على فترات لا")
                    {
                        PopulateSchoolPeriodGridview();
                    }
                }
            }
            catch
            {

            }
            ModalPopupExtender2.Enabled = true;
            ModalPopupExtender2.Show();
        }

        protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                //PeriodBL SPBL = new PeriodBL();

                OpenSISWCFServicePeriodClient SPClient = new OpenSISWCFServicePeriodClient();

                foreach (ReorderListItem RI in rlperiod.Items)
                {
                    if (RI.ItemType == ListItemType.Item || RI.ItemType == ListItemType.AlternatingItem)
                    {
                        HiddenField hdnPeriodId = (HiddenField)RI.FindControl("hdnPeriodId");
                        CheckBox chk = (CheckBox)RI.FindControl("chkrow");
                        if (chk.Checked)
                        {
                            
                            SPClient.DeletePeriod(Convert.ToInt32(hdnPeriodId.Value));
                            Session["datatable"] = null;
                            SPClient.Close();

                        }
                    }
                }
                SPClient.Close();
                PopulateSchoolPeriodGridview();
            }
            catch
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //Function Func = new Function();
            //try
            //{
            //    Func.ResetFormControlValues(this.Page);
            //    PopulateSchoolPeriodGridview();
            //    txtStartTime.Value = "12:00 pm";
            //    txtEndTime.Value = "12:00 pm";
            //}
            //catch
            //{

            //}
            //finally
            //{
            //    Func = null;
            //}
        }

        protected void btnSCancel_Click(object sender, EventArgs e)
        {
            PopulateSchoolPeriodGridview();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string UsedAtt = "";
                int Period_Id = Convert.ToInt32(Session["PeriodId"].ToString());
                if (ChkUsedAttenEdit.Checked)
                {
                    UsedAtt = "Y";
                }
                else
                {
                    UsedAtt = "N";
                }

                OpenSISWCFServicePeriodClient SPClient = new OpenSISWCFServicePeriodClient();

                if (txtShortNameEdit.Text.ToString() != "" && txtTitleEdit.Text.ToString() != "" && txtLengthEdit.Text != "")
                {
                    SPClient.UpdatePeriod(txtTitleEdit.Text, txtShortNameEdit.Text, Convert.ToInt32(txtLengthEdit.Text), txtBlockEdit.Text, UsedAtt, txtStartTimeEdit.Value, txtEndTimeEdit.Value, Period_Id);
                }

                SPClient.Close();
                PopulateSchoolPeriodGridview();
            }
            catch
            {

            }
            finally
            {
                ModalPopupExtender2.Enabled = false;
            }
        }

        protected void btnCancelEdit_Click(object sender, EventArgs e)
        {
            ModalPopupExtender2.Enabled = false;
            PopulateSchoolPeriodGridview();
        }
            
        protected void rlperiod_ItemReorder(object sender, AjaxControlToolkit.ReorderListItemReorderEventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                OpenSISWCFServicePeriodClient SPClient = new OpenSISWCFServicePeriodClient();

                int SchId = Convert.ToInt32(Session["SchoolID"]);
                dt = SPClient.PopulateSchoolPeriod(SchId,"","","",0,"","");

                if (dt != null)
                {
                    string strTempOrder = dt.Rows[e.OldIndex]["short_order"].ToString();
                    dt.Rows[e.OldIndex]["short_order"] = dt.Rows[e.NewIndex]["short_order"];
                    dt.Rows[e.NewIndex]["short_order"] = strTempOrder;
                    dt.AcceptChanges();

                    SPClient.UpdateShortOrder(dt.Rows[e.NewIndex]["Short_Order"].ToString(), Convert.ToInt32(dt.Rows[e.NewIndex]["Sch_period_id"].ToString()));

                    SPClient.UpdateShortOrder(dt.Rows[e.OldIndex]["Short_Order"].ToString(), Convert.ToInt32(dt.Rows[e.OldIndex]["Sch_period_id"].ToString()));
                   
                    DataTable neworderdt = new DataTable();
                    neworderdt = SPClient.PopulateSchoolPeriod(SchId, "", "", "", 0, "", "");
                    rlperiod.DataSource = neworderdt;
                    rlperiod.DataBind();                    
                }
                SPClient.Close();
            }
            catch
            { 

            }
        }

        protected void rlperiod_ItemDataBound(object sender, AjaxControlToolkit.ReorderListItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item)
                {

                    CheckBox UsedForAttendance = (CheckBox)e.Item.FindControl("UsedForAttendance");


                    HiddenField hdnUFA = (HiddenField)e.Item.FindControl("hdnUFA");


                    if (hdnUFA.Value.Trim() == "Y")
                    {
                        UsedForAttendance.Checked = true;
                    }
                }
            }
            catch { }
        }


        private void ml_InitCulture()
        {
            if (Session["MLCulture"] != null && Session["MLUICulture"] != null)
            {
                String MLCultureName = (String)Session["MLCulture"];
                String MLUICultureName = (String)Session["MLUICulture"];
                sCulture = Session["MLCulture"].ToString();
                if ((MLCultureName != null) && (MLCultureName.Length > 0))
                {
                    Page.Culture = MLCultureName;
                }

                if ((MLUICultureName != null) && (MLUICultureName.Length > 0))
                {
                    Page.UICulture = MLUICultureName;
                }
            }
        }

        protected override void InitializeCulture()
        {
            ml_InitCulture();
        }

    }
}